package com.oscchina.xushao.bean;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

import com.oscchina.xushao.app.AppException;
import com.oscchina.xushao.common.StringUtils;

/**
 * 帖子实体类
 *
 * @author liux (http://my.oschina.net/liux)
 * @version 1.0
 * @created 2012-3-21
 */
public class Post extends Entity {

    public final static String NODE_ID = "id";
    public final static String NODE_TITLE = "title";
    public final static String NODE_URL = "url";
    public final static String NODE_FACE = "portrait";
    public final static String NODE_BODY = "body";
    public final static String NODE_AUTHORID = "authorid";
    public final static String NODE_AUTHOR = "author";
    public final static String NODE_PUBDATE = "pubDate";
    public final static String NODE_ANSWERCOUNT = "answerCount";
    public final static String NODE_VIEWCOUNT = "viewCount";
    public final static String NODE_FAVORITE = "favorite";
    public final static String NODE_START = "post";

    public final static int CATALOG_ASK = 1;
    public final static int CATALOG_SHARE = 2;
    public final static int CATALOG_OTHER = 3;
    public final static int CATALOG_JOB = 4;
    public final static int CATALOG_SITE = 5;

    private String title;
    private String url;
    private String face;
    private String body;
    private String author;
    private int authorId;
    private int answerCount;
    private int viewCount;
    private String pubDate;
    private int catalog;
    private int isNoticeMe;
    private int favorite;
    private List<String> tags;


    public List<String> getTags() {
        return tags;
    }

    public void setTags(List<String> tags) {
        this.tags = tags;
    }

    public int getFavorite() {
        return favorite;
    }

    public void setFavorite(int favorite) {
        this.favorite = favorite;
    }

    public int getCatalog() {
        return catalog;
    }

    public void setCatalog(int catalog) {
        this.catalog = catalog;
    }

    public int getIsNoticeMe() {
        return isNoticeMe;
    }

    public void setIsNoticeMe(int isNoticeMe) {
        this.isNoticeMe = isNoticeMe;
    }

    public String getPubDate() {
        return this.pubDate;
    }

    public void setPubDate(String pubDate) {
        this.pubDate = pubDate;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getAuthorId() {
        return authorId;
    }

    public void setAuthorId(int authorId) {
        this.authorId = authorId;
    }

    public String getFace() {
        return face;
    }

    public void setFace(String face) {
        this.face = face;
    }

    public int getAnswerCount() {
        return answerCount;
    }

    public void setAnswerCount(int answerCount) {
        this.answerCount = answerCount;
    }

    public int getViewCount() {
        return viewCount;
    }

    public void setViewCount(int viewCount) {
        this.viewCount = viewCount;
    }

    public static Post parse(InputStream inputStream) throws IOException, AppException {
        Post post = null;
        // 获得XmlPullParser解析器
        XmlPullParser xmlParser = Xml.newPullParser();
        try {
            xmlParser.setInput(inputStream, UTF8);
            // 获得解析到的事件类别，这里有开始文档，结束文档，开始标签，结束标签，文本等等事件。
            int evtType = xmlParser.getEventType();
            // 一直循环，直到文档结束
            while (evtType != XmlPullParser.END_DOCUMENT) {
                String tag = xmlParser.getName();
                switch (evtType) {
                    case XmlPullParser.START_TAG:
                        if (tag.equalsIgnoreCase(NODE_START)) {
                            post = new Post();
                        } else if (post != null) {
                            if (tag.equalsIgnoreCase(NODE_ID)) {
                                post.id = StringUtils.toInt(xmlParser.nextText(), 0);
                            } else if (tag.equalsIgnoreCase(NODE_TITLE)) {
                                post.setTitle(xmlParser.nextText());
                            } else if (tag.equalsIgnoreCase(NODE_URL)) {
                                post.setUrl(xmlParser.nextText());
                            } else if (tag.equalsIgnoreCase(NODE_FACE)) {
                                post.setFace(xmlParser.nextText());
                            } else if (tag.equalsIgnoreCase(NODE_BODY)) {
                                post.setBody(xmlParser.nextText());
                            } else if (tag.equalsIgnoreCase(NODE_AUTHOR)) {
                                post.setAuthor(xmlParser.nextText());
                            } else if (tag.equalsIgnoreCase(NODE_AUTHORID)) {
                                post.setAuthorId(StringUtils.toInt(xmlParser.nextText(), 0));
                            } else if (tag.equalsIgnoreCase(NODE_ANSWERCOUNT)) {
                                post.setAnswerCount(StringUtils.toInt(xmlParser.nextText(), 0));
                            } else if (tag.equalsIgnoreCase(NODE_VIEWCOUNT)) {
                                post.setViewCount(StringUtils.toInt(xmlParser.nextText(), 0));
                            } else if (tag.equalsIgnoreCase(NODE_PUBDATE)) {
                                post.setPubDate(xmlParser.nextText());
                            } else if (tag.equalsIgnoreCase(NODE_FAVORITE)) {
                                post.setFavorite(StringUtils.toInt(xmlParser.nextText(), 0));
                            }
                            //标签
                            else if (tag.equalsIgnoreCase("tags")) {
                                post.tags = new ArrayList<String>();
                            } else if (post.getTags() != null && tag.equalsIgnoreCase("tag")) {
                                post.getTags().add(xmlParser.nextText());
                            }
                            //通知信息
                            else if (tag.equalsIgnoreCase("notice")) {
                                post.setNotice(new Notice());
                            } else if (post.getNotice() != null) {
                                if (tag.equalsIgnoreCase("atmeCount")) {
                                    post.getNotice().setAtmeCount(StringUtils.toInt(xmlParser.nextText(), 0));
                                } else if (tag.equalsIgnoreCase("msgCount")) {
                                    post.getNotice().setMsgCount(StringUtils.toInt(xmlParser.nextText(), 0));
                                } else if (tag.equalsIgnoreCase("reviewCount")) {
                                    post.getNotice().setReviewCount(StringUtils.toInt(xmlParser.nextText(), 0));
                                } else if (tag.equalsIgnoreCase("newFansCount")) {
                                    post.getNotice().setNewFansCount(StringUtils.toInt(xmlParser.nextText(), 0));
                                }
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        break;
                }
                //如果xml没有结束，则导航到下一个节点
                evtType = xmlParser.next();
            }
        } catch (XmlPullParserException e) {
            throw AppException.xml(e);
        } finally {
            inputStream.close();
        }
        return post;
    }

}
